<?php

require 'Provinces.php';
require 'Districts.php';

class Customers {

    private $_ID;
    private $_name;
    private $_provinceID;
    private $_districtID;
    private $_address;
    private $_phone;
    private $_email;
    private $_gender;
    
    private $_province;
    private $_district;

    public function getProvince() {
        return $this->_province;
    }

    public function setProvince($Province) {
        $this->_province = $Province;
    }

    public function getDistrict() {
        return $this->_district;
    }

    public function setDistrict($District) {
        $this->_district = $District;
    }

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getName() {
        return $this->_name;
    }

    public function setName($Name) {
        $this->_name = $Name;
    }

    public function getProvinceID() {
        return $this->_provinceID;
    }

    public function setProvinceID($ProvinceID) {
        $this->_provinceID = $ProvinceID;
    }

    public function getDistrictID() {
        return $this->_districtID;
    }

    public function setDistrictID($DistrictID) {
        $this->_districtID = $DistrictID;
    }

    public function getAddress() {
        return $this->_address;
    }

    public function setAddress($Address) {
        $this->_address = $Address;
    }

    public function getPhone() {
        return $this->_phone;
    }

    public function setPhone($Phone) {
        $this->_phone = $Phone;
    }

    public function getEmail() {
        return $this->_email;
    }

    public function setEmail($Email) {
        $this->_email = $Email;
    }

    public function getGender() {
        return $this->_gender;
    }

    public function setGender($Gender) {
        $this->_gender = $Gender;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `customers`
                                (
                                 `Cus_Name`,
                                 `Cus_P_ID`,
                                 `Cus_D_ID`,
                                 `Cus_Address`,
                                 `Cus_Phone`,
                                 `Cus_Email`,
                                 `Cus_Gender`)
                    VALUES (
                            :Cus_Name,
                            :Cus_P_ID,
                            :Cus_D_ID,
                            :Cus_Address,
                            :Cus_Phone,
                            :Cus_Email,
                            :Cus_Gender);";
            $parameters = array(
                ":Cus_Name",
                ":Cus_P_ID",
                ":Cus_D_ID",
                ":Cus_Address",
                ":Cus_Phone",
                ":Cus_Email",
                ":Cus_Gender",
            );

            $values = array(
                $this->getName(), $this->getProvinceID(),
                $this->getDistrictID(), $this->getAddress(),
                $this->getPhone(), $this->getEmail(),
                $this->getGender()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT
            );

            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `Cus_ID`,
                        `Cus_Name`,
                        `Cus_P_ID`,
                        `Cus_D_ID`,
                        `Cus_Address`,
                        `Cus_Phone`,
                        `Cus_Email`,
                        `Cus_Gender`
                      FROM `customers`
                      WHERE Cus_ID = :Cus_ID";

            $parameters = array(":Cus_ID",);

            $values = array($this->getID());

            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types, true);

            if ($result != null) {

                $row = $result->fetch();

                $this->setID($row->Cus_ID);
                $this->setName($row->Cus_Name);
                $this->setProvinceID($row->Cus_P_ID);
                $this->setDistrictID($row->Cus_D_ID);
                $this->setAddress($row->Cus_Address);
                $this->setPhone($row->Cus_Phone);
                $this->setEmail($row->Cus_Email);
                $this->setGender($row->Cus_Gender);

                if ($this->getProvinceID() != '') {
                    $province = new Provinces();
                    $province->setID($this->getProvinceID());
                    $province = $province->getByID($db);
                    $this->setProvince($province);
                }


                if ($this->getDistrictID() != '') {
                    $district = new Districts();
                    $district->setID($this->getDistrictID());
                    $district = $district->getByID($db);
                    $this->setDistrict($district);
                }


                return $this;
            } else {
                return null;
            }
        }
    }

}

?>
